package com.ruoyi.xuexiao;

import com.alibaba.fastjson2.JSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

public class CoordinateTransform {
    public static void main(String[] args) {
        // 输入的MULTIPOLYGON WKT
        String wkt = "MULTIPOLYGON (((38616767.05775486 4322707.336439, 38616768.065492205 4322707.162697567, 38616775.66446751 4322707.7473182175, 38616783.263358645 4322713.008313373, 38616844.0553954 4322715.931944033, 38616869.44489862 4322721.2559101, 38616884.40903002 4322728.84173454, 38616905.25019676 4322733.619566072, 38616909.056442894 4322734.492070021, 38616923.67228879 4322749.506002357, 38616927.60152625 4322754.019694325, 38616935.725365005 4322763.351723688, 38616942.314917274 4322770.921300482, 38616953.63391343 4322785.886348739, 38616964.19147086 4322796.612437906, 38616970.43221474 4322800.235856667, 38616977.268552326 4322804.205081224, 38616992.05685802 4322807.595492322, 38617007.08630628 4322813.634092886, 38617010.417888984 4322813.08138491, 38617020.0020184 4322810.682047695, 38617029.58607618 4322808.282720864, 38617032.97383102 4322804.9247924, 38617035.16903723 4322805.588568697, 38617041.78190828 4322800.786649983, 38617045.756809436 4322793.954587549, 38617046.355218925 4322793.899764448, 38617053.56231434 4322781.237959674, 38617057.7495856 4322773.052664245, 38617061.80586293 4322769.892831764, 38617065.600709714 4322763.563729518, 38617066.1362173 4322758.202635221, 38617077.547284864 4322749.256467853, 38617082.833794825 4322741.183595089, 38617083.46386087 4322732.957123194, 38617082.373986095 4322722.522491688, 38617073.02208446 4322712.532306273, 38617060.99754771 4322707.473996017, 38617051.52733532 4322701.065608971, 38617040.935471624 4322696.054485453, 38617034.10285464 4322685.788899209, 38617027.168585315 4322678.998280685, 38617023.25656878 4322670.930227658, 38617008.78394836 4322656.873965792, 38616996.32374599 4322643.563169047, 38616972.17749103 4322634.61920774, 38616949.774840124 4322634.55881121, 38616934.19892956 4322633.36585905, 38616917.40084294 4322628.058953658, 38616911.73903081 4322626.282974595, 38616902.11583293 4322618.04943414, 38616879.80278072 4322615.276284076, 38616867.67769286 4322604.902068771, 38616862.64818394 4322598.076347666, 38616855.54416826 4322590.704642346, 38616849.65768989 4322588.8064352125, 38616821.18717717 4322579.625737237, 38616807.77423839 4322580.747974541, 38616799.59087021 4322576.663215682, 38616793.0508226 4322565.996934045, 38616787.88206308 4322559.673128199, 38616784.612382114 4322557.82055827, 38616762.29928964 4322555.047448761, 38616731.66892654 4322557.431010872, 38616716.048295386 4322557.5945254415, 38616699.70459007 4322559.0919663105, 38616690.29949506 4322556.0657895245, 38616670.177716255 4322548.612387132, 38616656.79117781 4322540.36576716, 38616642.37303747 4322535.917810047, 38616623.19124281 4322537.675085004, 38616623.11009017 4322537.747785009, 38616614.15672188 4322538.616503445, 38616591.642123185 4322541.947054137, 38616575.25381455 4322544.800783537, 38616571.64542274 4322546.299315207, 38616570.626889236 4322546.476686185, 38616560.27009942 4322550.777991176, 38616550.907297306 4322552.42877117, 38616537.69547552 4322550.6198585965, 38616529.50565977 4322549.435222659, 38616510.26551621 4322550.628958157, 38616508.29861245 4322552.776063822, 38616502.07980211 4322556.403626794, 38616494.264923394 4322562.233321501, 38616487.995466396 4322568.085776679, 38616484.26260707 4322576.618119782, 38616481.06437119 4322583.837132642, 38616478.381300054 4322591.063864013, 38616477.680673555 4322603.604458632, 38616477.63575247 4322606.642440259, 38616473.41315106 4322613.450037859, 38616468.659852706 4322621.306680825, 38616461.73860139 4322636.397678783, 38616454.29253368 4322652.14144215, 38616447.29322869 4322672.5159466285, 38616446.2300339 4322676.774926102, 38616444.00719428 4322685.678942662, 38616442.7621806 4322700.193248408, 38616444.11230269 4322713.424779983, 38616447.82322712 4322741.22392378, 38616453.1379958 4322765.083270393, 38616458.628565684 4322777.054883603, 38616471.25259366 4322794.4166281745, 38616474.25093728 4322801.626605734, 38616491.15399352 4322842.272589777, 38616491.571783006 4322842.8483089935, 38616489.97719546 4322852.1638701465, 38616493.358390495 4322867.407112779, 38616498.34336991 4322878.710664034, 38616510.48138316 4322894.083538024, 38616514.24429406 4322900.604877707, 38616514.401719205 4322914.663561828, 38616513.64294894 4322924.164652832, 38616504.81332138 4322935.924488483, 38616495.97199863 4322948.476803262, 38616490.88625943 4322957.913899235, 38616490.675276816 4322972.179251103, 38616492.85381318 4322978.01932678, 38616493.6487196 4322980.15015782, 38616500.948974594 4322988.185163699, 38616503.31596259 4322995.354361728, 38616503.175290875 4323004.864596916, 38616503.617609546 4323016.761584628, 38616505.26091754 4323031.054466657, 38616511.30142801 4323040.656137157, 38616524.08263494 4323054.320951071, 38616532.00094318 4323062.365134224, 38616539.825423755 4323076.749383651, 38616545.77203706 4323092.691240938, 38616550.63485866 4323098.3121028, 38616552.20134047 4323100.383054139, 38616553.27954202 4323101.369016714, 38616557.18381245 4323104.939312577, 38616560.13130184 4323107.634698112, 38616574.83699237 4323116.571923964, 38616584.65189561 4323118.710645964, 38616589.58965362 4323122.339199893, 38616598.61491152 4323139.119421195, 38616601.523372665 4323146.916645417, 38616603.37214534 4323151.872947432, 38616606.04352196 4323162.330813302, 38616608.035414316 4323170.9666349925, 38616609.2110745 4323173.496792948, 38616615.70154739 4323187.46420777, 38616615.824358106 4323187.728576323, 38616616.837101966 4323188.925696585, 38616627.36892439 4323201.375365809, 38616643.28717527 4323211.916173654, 38616645.98022368 4323212.47556703, 38616655.61406793 4323214.476781042, 38616682.16914917 4323216.455546828, 38616699.5231434 4323213.5418954715, 38616707.61719373 4323209.698413331, 38616712.57328412 4323201.1710142465, 38616719.03680233 4323190.050242217, 38616721.720646866 4323175.821524136, 38616722.00253475 4323156.8009452205, 38616727.97818735 4323139.216949584, 38616732.99196136 4323124.463312138, 38616744.34126311 4323109.570295614, 38616753.33555119 4323086.715443091, 38616757.16469334 4323071.378612228, 38616758.115745425 4323069.822402684, 38616761.29092494 4323060.297424128, 38616763.93692718 4323047.068225692, 38616766.053791866 4323034.897463128, 38616769.2289748 4323021.139126899, 38616775.05001513 4323006.851676386, 38616781.400179 4322996.268450901, 38616781.92951572 4322986.214203371, 38616771.176427364 4322975.812420359, 38616768.54606401 4322973.620392641, 38616762.40850384 4322968.3594923485, 38616763.285443045 4322957.837727549, 38616763.05737344 4322950.765763879, 38616762.420016736 4322950.044064425, 38616755.07298934 4322945.178990284, 38616747.0842932 4322941.889800023, 38616734.12708311 4322940.112367624, 38616719.33901412 4322936.722438046, 38616710.76743775 4322931.046516994, 38616700.31794036 4322926.928268906, 38616684.946889736 4322921.1516371425, 38616686.89516215 4322914.838951046, 38616693.15871723 4322909.382939868, 38616698.15072027 4322906.286025114, 38616711.77427131 4322898.207516635, 38616719.835111015 4322875.427978074, 38616730.10106005 4322850.213913441, 38616733.7734267 4322836.9021494035, 38616734.45688495 4322834.424593458, 38616735.43764489 4322823.869813362, 38616740.5864047 4322810.206130965, 38616744.94219234 4322794.416715772, 38616740.247659154 4322777.436374327, 38616736.95899265 4322758.138488314, 38616736.455569185 4322755.184654238, 38616731.60098913 4322741.206144763, 38616731.23915297 4322732.881770024, 38616734.16193789 4322727.621053239, 38616741.17649638 4322718.853133567, 38616751.11379425 4322710.085296734, 38616767.05775486 4322707.336439)))";

        // 创建WKTReader和GeometryFactory
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        WKTReader reader = new WKTReader(geometryFactory);

        try {
            // 解析WKT字符串为Geometry对象
            Geometry geometry = reader.read(wkt);
            // 定义源坐标系（EPSG:3857）和目标坐标系（EPSG:4326）
            CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3857");
            CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
            // 创建坐标转换MathTransform
            MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);
            // 执行坐标转换
            Geometry targetGeometry = org.geotools.geometry.jts.JTS.transform(geometry, transform);
            // 将转换后的Geometry对象转换为GeoJSON字符串
            StringWriter writer = new StringWriter();
            GeometryJSON gjson = new GeometryJSON();
            gjson.write(targetGeometry, writer);
            // 输出GeoJSON字符串
            String json = writer.toString();
            Map o = JSON.parseObject(json, Map.class);
            String o1 = o.get("coordinates").toString();
            System.out.println("(((" + o1.replaceAll("\\[", "").replaceAll("\\]", "") + ")))");
        } catch (ParseException | TransformException | FactoryException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
